package de.fhtrier.mp.wollmaus.gamestate.menus;

import java.awt.Point;
import java.awt.Polygon;
import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.state.transition.Transition;

import de.fhtrier.mp.wollmaus.MainGame;
import de.fhtrier.mp.wollmaus.constants.Constants;
import de.fhtrier.mp.wollmaus.gamestate.AbstractState;
import de.fhtrier.mp.wollmaus.gamestate.IntroState;
import de.fhtrier.mp.wollmaus.util.AssetManager;

public class MenuState extends AbstractState {
	
	private AbstractSubMenuState menuState;
	
	private Polygon button1;
	private Polygon button2;
	private Polygon button3;
	private Polygon button4;
	private Polygon button5;
	private Polygon buttonBack;
	
	private ArrayList<Polygon> buttons;
	
	public static MenuState instance;

	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		menuState = new MainState(this);
		
		instance = this;
		
		buttons = new ArrayList<Polygon>();
		
		button1 = new Polygon();
		button1.addPoint(640,402);
		button1.addPoint(640,402+65);
		button1.addPoint(640+215,402+65);
		button1.addPoint(640+215,402);
		buttons.add(button1);
		
		button2 = new Polygon();
		button2.addPoint(640,471);
		button2.addPoint(640,471+65);
		button2.addPoint(640+242,471+65);
		button2.addPoint(640+242,471);
		buttons.add(button2);
		
		button3 = new Polygon();
		button3.addPoint(640,538);
		button3.addPoint(640,538+65);
		button3.addPoint(640+138,538+65);
		button3.addPoint(640+138,538);
		buttons.add(button3);
		
		button4 = new Polygon();
		button4.addPoint(640,604);
		button4.addPoint(640,604+65);
		button4.addPoint(640+174,604+65);
		button4.addPoint(640+174,604);
		buttons.add(button4);
		
		button5 = new Polygon();
		button5.addPoint(640,670);
		button5.addPoint(640,670+65);
		button5.addPoint(640+138,670+65);
		button5.addPoint(640+138,670);
		buttons.add(button5);
		
		buttonBack = new Polygon();
		buttonBack.addPoint(921,101);
		buttonBack.addPoint(921,101+48);
		buttonBack.addPoint(921+48,101+48);
		buttonBack.addPoint(921+48,101);
		buttons.add(buttonBack);
	}

	@Override
	public int getID() {
		return 1;
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		menuState.getImage().draw();
		menuState.render(g, gc);
		
		if (Constants.debug == true) {
			for (Polygon b : buttons) {
				for (int i=0;i<b.npoints-1;++i) {
					g.setColor(Color.blue);
					g.drawLine(b.xpoints[i], b.ypoints[i], b.xpoints[i+1], b.ypoints[i+1]);
				}
				g.drawLine(b.xpoints[0], b.ypoints[0], b.xpoints[b.npoints-1], b.ypoints[b.npoints-1]);
			}
		}
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {		
		super.update(gc, sbg, delta);
		menuState.update(gc, sbg, delta);
		
		if (gc.getInput().isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
			Point mousePoint = new Point(gc.getInput().getAbsoluteMouseX(),gc.getInput().getAbsoluteMouseY());
			
			if (button1.contains(mousePoint)) {
				menuState.button1Pressed(gc, sbg, delta);
			}
			
			if (button2.contains(mousePoint)) {
				menuState.button2Pressed(gc, sbg, delta);
			}
			
			if (button3.contains(mousePoint)) {
				menuState.button3Pressed(gc, sbg, delta);
			}
			
			if (button4.contains(mousePoint)) {
				menuState.button4Pressed(gc, sbg, delta);
			}
			
			if (button5.contains(mousePoint)) {
				menuState.button5Pressed(gc, sbg, delta);
			}
			
			if (buttonBack.contains(mousePoint)) {
				menuState.buttonBackPressed(gc, sbg, delta);
			}
		}
	}
	
	public void startGameStart(StateBasedGame sbg) {
		MainGame.instance.bgMusic.stop();
		Transition transitionOut = new FadeOutTransition(Color.black, 1000);
		Transition transitionIn = new FadeInTransition(Color.black, 1000);
		IntroState.instance.preEnter();
		sbg.enterState(0, transitionOut, transitionIn);
	}
	
	public void exit(GameContainer gc) {
		gc.exit();
	}

	@Override
	public void enter(GameContainer gc, StateBasedGame sbg) throws SlickException {
		if (MainGame.instance.bgMusic != null)
			MainGame.instance.bgMusic.stop();
		MainGame.instance.bgMusic = AssetManager.getInstance().getMusic(AssetManager.MUSIC_MENU);
		MainGame.instance.bgMusic.loop(1f, Constants.MUSIC_VOLUME);
	}

	@Override
	public void leave(GameContainer gc, StateBasedGame sbg) throws SlickException {
	}

	public void setMenuState(AbstractSubMenuState menuState) {
		this.menuState = menuState;
	}

}
